package pl.vegasoft.jpgtimename;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

public class FileResolver {
	
	private static final String DIR_KEY = "directory";
	
	private File m_dir = new File(".");
	
	private Preferences m_prefs;
	
	public FileResolver() {
		m_prefs = Preferences.userNodeForPackage(JpgTimeName.class);
		String dir = m_prefs.get(DIR_KEY, ".");
		m_dir = new File(dir);
	}
	
	public File[] getFiles() {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setCurrentDirectory(m_dir);
		fileChooser.setMultiSelectionEnabled(true);
		FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG Images", "jpg");
		fileChooser.setFileFilter(filter);
		int response = fileChooser.showOpenDialog(null);
		if (response == JFileChooser.APPROVE_OPTION) {
			List<File> files = new ArrayList<File>();
			
			for(File selected : fileChooser.getSelectedFiles()) {
				if (selected.isDirectory()) {
					files.addAll(getFilesFromDir(selected));
				} else {
					files.add(selected);
				}
			}
			
			m_prefs.put(DIR_KEY, fileChooser.getCurrentDirectory().getAbsolutePath());
			
			return files.toArray(new File[files.size()]);
		} else {
			return new File[0];
		}
	}
	
	private List<File> getFilesFromDir(File dir) {
		List<File> filesList = new ArrayList<File>();
		for(File file : dir.listFiles()) {
			if (file.isFile()) {
				filesList.add(file);
			} else {
				filesList.addAll(getFilesFromDir(file));
			}
		}
		
		return filesList;
	}

}
